package com.tianji.learning.delaytask;

import com.tianji.learning.service.IPointsBoardService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class PointsBoardTask {

    @Autowired
    private IPointsBoardService pointsBoardService;

    @Autowired
    private StringRedisTemplate  redisTemplate;

    //创建赛季任务
    @XxlJob("createTableJob")
    public ReturnT<String> createTableJob(){
        log.info("==========开始创建数据表============");
        pointsBoardService.createTable();
        return ReturnT.SUCCESS;
    }

    // 持久化到MySQL任务
    @XxlJob("savePointsBoard2DB")
    public ReturnT<String> savePointsBoard2DB(String param){
        log.info("========开始持久化数据表=====");
        pointsBoardService.savePointBoard2DB();
        return ReturnT.SUCCESS;
    }

    //清理redis中的上个月榜单信息
    @XxlJob("clearPointsBoardFromRedis")
    public ReturnT<String> clearPointsBoardFromRedis(){
        log.info("===============开始清理榜单数据=====================");
        //1. 获取上月时间
        LocalDate lastDate = LocalDate.now().minusMonths(1);
        String lastDateSeason = lastDate.format(DateTimeFormatter.ofPattern("yyyyMM"));
        //2. 异步删除
        redisTemplate.unlink("boards:"+lastDateSeason);
        return ReturnT.SUCCESS;
    }


}
